package org.hgvc.webapp.ets.jsf.beans.member;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.bo.MemberIndAcct;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.Race;
import org.hgvc.webapp.ets.bo.Salutation;
import org.hgvc.webapp.ets.bo.State;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.BeanUtil;
import org.jocsb.base.util.FacesUtils;
import org.jocsb.base.util.SpringUtils;



public class MemberIndProspectProductAsGift extends AbstractBean {

	public static final String BEAN_NAME="memberIndProspectProductAsGift";
	public static final String NAV_CREATION="memberIndProductAsGift";
	public static final String NAV_DEPENDANT_CREATION="memberIndDependantProductAsGift";
	private MemberService service;
	private MasterSetupService setupService;
	private Member member;
	private MemberIndAcct memberIndAcct;
	private MemberInd memberInd;
	private List<MemberIndAcct> dependantList;
	private List<SelectItem> salutationSelectList;
	private String salutationId;
	private List<SelectItem> raceSelectList;
	private List<SelectItem> maritalSelectList;
	private List<SelectItem> sexSelectList;
	private String raceId;
	private String indResCountryId;
	private List<SelectItem> countrySelectList;
	private String indResStateId;
	private List<SelectItem> indResStateSelectList;
	private boolean compFlag;
	private String indCompcountryId;
	private String indCompStateId;
	private List<SelectItem> indCompStateSelectList;
	private List<SelectItem> mailSelectList;
	private boolean mailFlag;
	private String mailSelect =CommonConstant.MAIL_SELECT_OTHER;
	private String indMailCountryId;
	private String indMailStateId;
	private List<SelectItem> indMailStateSelectList;
	private String deSaluationId;
	private String deRaceId;
	private String deResCountryId;
	private String deResStateId;
	private List<SelectItem> deResStateSelectList;
	private String deCompCountryId;
	private boolean deCompFlag;
	private String deCompStateId;
	private List<SelectItem> deCompStateSelectList;
	private String deMailSelect =CommonConstant.MAIL_SELECT_OTHER;;
	private boolean deMailFlag;
	private String deMailCountryId;
	private String deMailStateId;
	private List<SelectItem> deMailStateSelectList;
	private Membership membership;
	private boolean checkFlag = true;
	private String flag;


	public boolean isCheckFlag() {
		return checkFlag;
	}
	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public Membership getMembership() {
		return membership;
	}
	public void setMembership(Membership membership) {
		this.membership = membership;
	}
	public String getDeSaluationId() {
		return deSaluationId;
	}
	public void setDeSaluationId(String deSaluationId) {
		this.deSaluationId = deSaluationId;
	}
	public String getDeRaceId() {
		return deRaceId;
	}
	public void setDeRaceId(String deRaceId) {
		this.deRaceId = deRaceId;
	}
	public String getDeResCountryId() {
		return deResCountryId;
	}
	public void setDeResCountryId(String deResCountryId) {
		this.deResCountryId = deResCountryId;
	}
	public String getDeResStateId() {
		return deResStateId;
	}
	public void setDeResStateId(String deResStateId) {
		this.deResStateId = deResStateId;
	}
	public String getDeCompCountryId() {
		return deCompCountryId;
	}
	public void setDeCompCountryId(String deCompCountryId) {
		this.deCompCountryId = deCompCountryId;
	}
	public boolean isDeCompFlag() {
		return deCompFlag;
	}
	public void setDeCompFlag(boolean deCompFlag) {
		this.deCompFlag = deCompFlag;
	}
	public String getDeCompStateId() {
		return deCompStateId;
	}
	public void setDeCompStateId(String deCompStateId) {
		this.deCompStateId = deCompStateId;
	}
	public boolean isDeMailFlag() {
		return deMailFlag;
	}
	public void setDeMailFlag(boolean deMailFlag) {
		this.deMailFlag = deMailFlag;
	}
	public String getDeMailCountryId() {
		return deMailCountryId;
	}
	public void setDeMailCountryId(String deMailCountryId) {
		this.deMailCountryId = deMailCountryId;
	}
	public String getDeMailStateId() {
		return deMailStateId;
	}
	public void setDeMailStateId(String deMailStateId) {
		this.deMailStateId = deMailStateId;
	}
	public String getDeMailSelect() {
		return deMailSelect;
	}
	public String getIndMailCountryId() {
		return indMailCountryId;
	}
	public void setIndMailCountryId(String indMailCountryId) {
		this.indMailCountryId = indMailCountryId;
	}
	public String getIndMailStateId() {
		return indMailStateId;
	}
	public void setIndMailStateId(String indMailStateId) {
		this.indMailStateId = indMailStateId;
	}
	public boolean isMailFlag() {
		return mailFlag;
	}
	public void setMailFlag(boolean mailFlag) {
		this.mailFlag = mailFlag;
	}
	public String getMailSelect() {
		return mailSelect;
	}
	public String getIndCompStateId() {
		return indCompStateId;
	}
	public void setIndCompStateId(String indCompStateId) {
		this.indCompStateId = indCompStateId;
	}
	public String getIndCompcountryId() {
		return indCompcountryId;
	}
	public void setIndCompcountryId(String indCompcountryId) {
		this.indCompcountryId = indCompcountryId;
	}
	public boolean isCompFlag() {
		return compFlag;
	}
	public void setCompFlag(boolean compFlag) {
		this.compFlag = compFlag;
	}
	public String getIndResStateId() {
		return indResStateId;
	}
	public void setIndResStateId(String indResStateId) {
		this.indResStateId = indResStateId;
	}
	public String getIndResCountryId() {
		return indResCountryId;
	}
	public void setIndResCountryId(String indResCountryId) {
		this.indResCountryId = indResCountryId;
	}
	public String getRaceId() {
		return raceId;
	}
	public void setRaceId(String raceId) {
		this.raceId = raceId;
	}
	public String getSalutationId() {
		return salutationId;
	}
	public void setSalutationId(String salutationId) {
		this.salutationId = salutationId;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public MemberIndAcct getMemberIndAcct() {
		return memberIndAcct;
	}
	public void setMemberIndAcct(MemberIndAcct memberIndAcct) {
		this.memberIndAcct = memberIndAcct;
	}
	public MemberInd getMemberInd() {
		return memberInd;
	}
	public void setMemberInd(MemberInd memberInd) {
		this.memberInd = memberInd;
	}
	public List<MemberIndAcct> getDependantList() {
		return dependantList;
	}
	public void setDependantList(List<MemberIndAcct> dependantList) {
		this.dependantList = dependantList;
	}
	public void setService(MemberService service) {
		this.service = service;
	}
	public void setSetupService(MasterSetupService setupService) {
		this.setupService = setupService;
	}
	/*------------------------------------------------------------------------------------*/
	public String navCreateAction(){
		member = new Member();
		member.setRegistrationType(MemberConstant.MEMBER_REG_GIFT);
		member.setMemberType(CommonConstant.MEMBER_TYPE_IND);
		memberInd = new MemberInd();
		dependantList = new ArrayList<MemberIndAcct>();
		return NAV_CREATION;
	}
	
	public String navCreateDependantAction(){
		memberIndAcct = new MemberIndAcct();
		return NAV_DEPENDANT_CREATION;
	}
	
	public String navBackAction(){
		return NAV_CREATION;
	}
	
	public List<SelectItem> getSalutationSelectList() {
		try{
			List<Salutation> sList = setupService.getSalutationList();
			salutationSelectList= new ArrayList<SelectItem>();
			for(Salutation s:sList){
				SelectItem item = new SelectItem(s.getSalutationId(),s.getDesc());
				salutationSelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return salutationSelectList;
	}
	public List<SelectItem> getSexSelectList() {
		sexSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.genderSelectList){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			sexSelectList.add(item);
		}
		return sexSelectList;
	}
	public List<SelectItem> getMaritalSelectList() {
		maritalSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.maritalStatusSelectList){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			maritalSelectList.add(item);
		}
		return maritalSelectList;
	}
	public List<SelectItem> getRaceSelectList() {
		try{
			List<Race> rList = setupService.getRaceList();
			raceSelectList= new ArrayList<SelectItem>();
			for(Race r:rList){
				SelectItem item = new SelectItem(r.getRaceId(),r.getDesc());
				raceSelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return raceSelectList;
	}
	
	public List<SelectItem> getCountrySelectList() {
		try{
			List<Country> cList = setupService.getCountryList();
			countrySelectList= new ArrayList<SelectItem>();
			for(Country c:cList){
				SelectItem item = new SelectItem(c.getCountryId(),c.getDesc());
				countrySelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return countrySelectList;
	}
	public List<SelectItem> getIndResStateSelectList() {
		try{
			indResStateSelectList = new ArrayList<SelectItem>();
			if(indResCountryId!=null&&indResCountryId.length()>0){
				List<State> sList = setupService.getStateList(indResCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					indResStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return indResStateSelectList;
	}
	
	public List<SelectItem> getIndCompStateSelectList() {
		try{
			indCompStateSelectList = new ArrayList<SelectItem>();
			if(indCompcountryId!=null&&indCompcountryId.length()>0){
				List<State> sList = setupService.getStateList(indCompcountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					indCompStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return indCompStateSelectList;
	}
	public List<SelectItem> getMailSelectList() {

		mailSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.MAIL_SELECT_LIST){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			mailSelectList.add(item);
		}

		return mailSelectList;
	}
	public void setMailSelect(String mailSelect) {
		if(mailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_OTHER)){
			mailFlag = false;
			compFlag = false;
		}else{
			mailFlag = true;
			if(mailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
				memberInd.setMailAddress1(memberInd.getCompAddress1());
				memberInd.setMailAddress2(memberInd.getCompAddress2());
				memberInd.setMailPostcode(memberInd.getCompPostcode());
				memberInd.setMailCity(memberInd.getCompCity());
				memberInd.setMailCountry(memberInd.getCompCountry());
				memberInd.setMailState(memberInd.getCompState());
				compFlag = true;
			}else if(mailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_RES)){
				System.out.println(" setting------------------>>>>>>>>>>>.. ");
				memberInd.setMailAddress1(memberInd.getResAddress1());
				memberInd.setMailAddress2(memberInd.getResAddress2());
				memberInd.setMailPostcode(memberInd.getResPostcode());
				memberInd.setMailCity(memberInd.getResCity());
				memberInd.setMailCountry(memberInd.getResCountry());
				memberInd.setMailState(memberInd.getResState());
				compFlag = false;
			}
		}
		this.mailSelect = mailSelect;
	}
	public List<SelectItem> getIndMailStateSelectList() {
		try{
			indMailStateSelectList = new ArrayList<SelectItem>();
			if(indMailCountryId!=null&&indMailCountryId.length()>0){
				List<State> sList = setupService.getStateList(indMailCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					indMailStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return indMailStateSelectList;
	}
	public List<SelectItem> getDeResStateSelectList() {
		try{
			deResStateSelectList = new ArrayList<SelectItem>();
			if(deResCountryId!=null&&deResCountryId.length()>0){
				List<State> sList = setupService.getStateList(deResCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					deResStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return deResStateSelectList;
	}
	public List<SelectItem> getDeCompStateSelectList() {
		try{
			deCompStateSelectList = new ArrayList<SelectItem>();
			if(deCompCountryId!=null&&deCompCountryId.length()>0){
				List<State> sList = setupService.getStateList(deMailCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					deCompStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return deCompStateSelectList;
	}
	public List<SelectItem> getDeMailStateSelectList() {
		try{
			deMailStateSelectList = new ArrayList<SelectItem>();
			if(deMailCountryId!=null&&deMailCountryId.length()>0){
				List<State> sList = setupService.getStateList(deMailCountryId);
				for(State s:sList){
					SelectItem item = new SelectItem(s.getStateId(),s.getDesc());
					deMailStateSelectList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return deMailStateSelectList;
	}
	public void setDeMailSelect(String deMailSelect) {
		if(deMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_OTHER)){
			deMailFlag = false;
			deCompFlag = false;
		}else{
			deMailFlag = true;
			if(deMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
				memberIndAcct.setMailAddress1(memberIndAcct.getCompAddress1());
				memberIndAcct.setMailAddress2(memberIndAcct.getCompAddress2());
				memberIndAcct.setMailPostcode(memberIndAcct.getCompPostcode());
				memberIndAcct.setMailCity(memberIndAcct.getCompCity());
				memberIndAcct.setMailCountry(memberIndAcct.getCompCountry());
				memberIndAcct.setMailState(memberIndAcct.getCompState());
				deCompFlag = true;
			}else if(deMailSelect.equalsIgnoreCase(CommonConstant.MAIL_SELECT_RES)){
				memberIndAcct.setMailAddress1(memberIndAcct.getResAddress1());
				memberIndAcct.setMailAddress2(memberIndAcct.getResAddress2());
				memberIndAcct.setMailPostcode(memberIndAcct.getResPostcode());
				memberIndAcct.setMailCity(memberIndAcct.getResCity());
				memberIndAcct.setMailCountry(memberIndAcct.getResCountry());
				memberIndAcct.setMailState(memberIndAcct.getResState());
				deCompFlag = false;
			}
		}
		this.deMailSelect = deMailSelect;
	}
	
	public String createDependantAction(){
		
		if(dependantList.size()==membership.getMembershipCategory().getDependantLimitInt()){
			initErrorBean(SpringUtils.getMessage("error.dependant.limit", new Object[]{}));
			return NAV_DEPENDANT_CREATION;
		}

		//validate dependant ic no
		for(MemberIndAcct dependant:dependantList){
			if(dependant.getNewIcNo().equalsIgnoreCase(memberIndAcct.getNewIcNo())){
				initErrorBean(SpringUtils.getMessage("error.duplicate.ic.no.dependant.found", new Object[]{}));
				return NAV_DEPENDANT_CREATION;
			}
		}

		if(deMailCountryId!=null&&deMailCountryId.length()>0)
			memberIndAcct.setMailCountry(new Country(deMailCountryId));
		if(deMailStateId!=null&&deMailStateId.length()>0)
			memberIndAcct.setMailState(new State(deMailStateId));
		if(deCompCountryId!=null&&deCompCountryId.length()>0)
			memberIndAcct.setCompCountry(new Country(deCompCountryId));
		if(deCompStateId!=null&&deCompStateId.length()>0)
			memberIndAcct.setCompState(new State(deCompStateId));
		if(deResCountryId!=null&&deResCountryId.length()>0)
			memberIndAcct.setResCountry(new Country(deResCountryId));
		if(deResStateId!=null&&deResStateId.length()>0)
			memberIndAcct.setResState(new State(deResStateId));
		if(deRaceId!=null&&deRaceId.length()>0)
			memberIndAcct.setRace(new Race(deRaceId));

		MemberIndAcct newDependant = new MemberIndAcct();
		BeanUtil.copyFields(memberIndAcct, newDependant, true);
		memberIndAcct = new MemberIndAcct();
		dependantList.add(newDependant);
		
		return NAV_CREATION;
	}
	
	public String navPaymentOption(){
		
		if(indMailCountryId!=null&&indMailCountryId.length()>0)
			memberInd.setMailCountry(new Country(indMailCountryId));
		if(indMailStateId!=null&&indMailStateId.length()>0)
			memberInd.setMailState(new State(indMailStateId));
		if(indCompcountryId!=null&&indCompcountryId.length()>0)
			memberInd.setCompCountry(new Country(indCompcountryId));
		if(indCompStateId!=null&&indCompStateId.length()>0)
			memberInd.setCompState(new State(indCompStateId));
		if(indResCountryId!=null&&indResCountryId.length()>0)
			memberInd.setResCountry(new Country(indResCountryId));
		if(indResStateId!=null&&indResStateId.length()>0)
			memberInd.setResState(new State(indResStateId));
		if(raceId!=null&&raceId.length()>0)
			memberInd.setRace(new Race(raceId));
		if(salutationId!=null&&salutationId.length()>0)
			memberInd.setSalutation(new Salutation(salutationId));
		
		MemberGiftProspectPaymentBean bean = (MemberGiftProspectPaymentBean)FacesUtils.getManagedBean(MemberGiftProspectPaymentBean.BEAN_NAME);
		bean.setList(this.dependantList);
		bean.setMember(member);
		bean.setMemberInd(memberInd);
		bean.setMembership(membership);
		
		return bean.navSelectOptionAction();
	}
	
	public String backAction(){
		return MemberPortalBean.BEAN_NAME;
	}
	
	public void validator(FacesContext context,UIComponent comp,Object obj){
		String value = (String)obj;
		if(value.equalsIgnoreCase("1")){
			checkFlag = false;
		}else{
			checkFlag = true;
		}
	}
}
